package org.dochandler.dao.derby;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.dochandler.config.DHConfig;
import org.dochandler.dao.DocumentDAO;
import org.dochandler.service.domain.Document;
import org.dochandler.service.domain.Sensitivity;
import org.dochandler.service.exception.DatabaseException;

@SuppressWarnings("serial")
public class DerbyDocumentDAO implements DocumentDAO, Serializable {
	
	private Connection conn;
	private String derbyUrl;
	private String derbyDriver;
	
	public DerbyDocumentDAO() {
		if (conn == null) {
			this.derbyUrl = DHConfig.getString("local_db_url");
			this.derbyDriver = DHConfig.getString("local_db_driver");
	
			try {
				Class.forName(derbyDriver).newInstance();
				conn = DriverManager.getConnection(derbyUrl);
			} catch (Exception e) {
				throw new DatabaseException("Could not connect to the database", e);
			}
		}
	}
	
	@Override
	public Document findById(Long entityId) {
		Document doc = null;
		try {
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery("SELECT * FROM DOCUMENTS WHERE id =" + entityId);
			
			if (rs.next()) {
				doc = new Document();
				
				doc.setUniqueCode( rs.getLong("ID") );
				doc.setTitle( rs.getString("TITLE") );
				doc.setCreationDate( rs.getDate("CREATION_DATE") );
				doc.setCreatorId( rs.getLong("CREATOR_ID") );
				
				String sensitivityName = rs.getString("SENSITIVITY");
				doc.setSensitivity( Sensitivity.fromValue(sensitivityName) );
 			}
			
		} catch (SQLException e) {
			throw new DatabaseException("There has been a problem trying to find a document with the id: " + entityId, e);
		}
		return doc;
	}

	@Override
	public List<Document> findAll() {
		List<Document> docList = new ArrayList<Document>();
		String sql = "SELECT * FROM DOCUMENTS";
		try {
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			
			while (rs.next()) {
				Document document = new Document();
				
				document.setUniqueCode( rs.getLong("ID") );
				document.setTitle( rs.getString("TITLE") );
				document.setCreationDate( rs.getDate("CREATION_DATE") );
				document.setCreatorId( rs.getLong("CREATOR_ID") );
				
				String sensitivityName = rs.getString("SENSITIVITY");
				document.setSensitivity( Sensitivity.fromValue(sensitivityName) );
				
				docList.add( document );
				
 			}
			
		} catch (SQLException e) {
			throw new DatabaseException("There has been a problem trying to find all documents", e);
		}
		return docList;
	}

	@Override
	public boolean create(Document document) {
		String sql = "INSERT INTO DOCUMENTS (ID, TITLE, CONTENT, SENSITIVITY, CREATION_DATE, CREATOR_ID) VALUES(?, ?, ?, ?, ?, ?)";
		try {
			PreparedStatement stmt = conn.prepareStatement(sql);
			
			int i = 0;
			stmt.setLong(++i, document.getUniqueCode() );
			stmt.setString(++i, document.getTitle() );
			stmt.setString(++i, document.getContent() );
			stmt.setString(++i, document.getSensitivity().toString() );
			stmt.setDate(++i, new java.sql.Date( document.getCreationDate().getTime() ) );
			stmt.setLong(++i, document.getCreatorId() );
			
			stmt.execute();
			
		} catch (SQLException e) {
			throw new DatabaseException("There has been a problem trying to create this document", e);
		}
		
		return true;
	}

	@Override
	public boolean delete(Long entityId) {
		return false;
	}

	@Override
	public boolean update(Document entity) {
		return false;
	}
}